package tools;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MageKeyControl extends KeyAdapter {
    private boolean leftPressed = false;
    private boolean rightPressed = false;
    private boolean upPressed = false;
    private boolean downPressed = false;
    private boolean shootPressed = false;
    private boolean actionPressed = false;

    public void keyPressed(KeyEvent key) {
        switch (key.getKeyCode()) {
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_A:
            leftPressed = true;
            break;
        case KeyEvent.VK_RIGHT:
        case KeyEvent.VK_D:
            rightPressed = true;
            break;
        case KeyEvent.VK_UP:
        case KeyEvent.VK_W:
            upPressed = true;
            break;
        case KeyEvent.VK_DOWN:
        case KeyEvent.VK_S:
            downPressed = true;
            break;
        case KeyEvent.VK_SPACE:
        case KeyEvent.VK_X:
            shootPressed = true;
            break;
        case KeyEvent.VK_CONTROL:
        case KeyEvent.VK_Z:
            actionPressed = true;
            break;
        }
    }

    public void keyReleased(KeyEvent key) {
        switch (key.getKeyCode()) {
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_A:
            leftPressed = false;
            break;
        case KeyEvent.VK_RIGHT:
        case KeyEvent.VK_D:
            rightPressed = false;
            break;
        case KeyEvent.VK_UP:
        case KeyEvent.VK_W:
            upPressed = false;
            break;
        case KeyEvent.VK_DOWN:
        case KeyEvent.VK_S:
            downPressed = false;
            break;
        case KeyEvent.VK_SPACE:
        case KeyEvent.VK_X:
            shootPressed = false;
            break;
        case KeyEvent.VK_CONTROL:
        case KeyEvent.VK_Z:
            actionPressed = false;
            break;
        }
    }

    public Direction getDir() {
        if (upPressed && !downPressed) {
            if (leftPressed && !rightPressed) {
                return Direction.UP_LEFT;
            } else if (rightPressed && !leftPressed) {
                return Direction.UP_RIGHT;
            } else {
                return Direction.UP;
            }
        } else if (downPressed && !upPressed) {
            if (leftPressed && !rightPressed) {
                return Direction.DOWN_LEFT;
            } else if (rightPressed && !leftPressed) {
                return Direction.DOWN_RIGHT;
            } else {
                return Direction.DOWN;
            }
        } else {
            if (leftPressed && !rightPressed) {
                return Direction.LEFT;
            } else if (rightPressed && !leftPressed) {
                return Direction.RIGHT;
            }
        }
        return null;
    }

    public double getSpd() {
        return ((leftPressed ^ rightPressed) || (upPressed ^ downPressed)) ? 1
                : 0;
    }

    public boolean getShoot() {
        return shootPressed;
    }

    public boolean getAction() {
        return actionPressed;
    }
}
